package com.bitsbazaar.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class BaseDaoImpl implements BaseDao {


	//protected HibernateTemplate template = null;
	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
		//template = new HibernateTemplate(sessionFactory);
	}



	//private Session session;



	@Override
	@Transactional(propagation = Propagation.REQUIRED) 
	/**
	 * Saves the object, returns the key(id) generated if success
	 * return -1 otherwise
	 */
	public Long save(Object obj) {
//		try
//		{
			return (Long) getSessionFactory().getCurrentSession().save(obj);
//		}
//		catch(Exception e)
//		{
//			return (long) -1;
//		}

	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED) 
	/**
	 * Updates the given object
	 * 
	 * @return true if success, false if failure/exception
	 * @author Rachit
	 */
	public Boolean update(Object obj) {
		try
		{
			getSessionFactory().getCurrentSession().update(obj);
			return true;
		}
		catch(Exception e)
		{
			return false;
		}

	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED) 
	/**
	 * Delete the element of the Class with the key id
	 * 
	 * @return true if success, false if failure
	 * @author Rachit
	 */
	public Boolean deleteById(Class class1,Long id) {
		try
		{
			Session session = getSessionFactory().getCurrentSession();
			Object objToDelete = session.get(class1, id);
			if(objToDelete != null)
			{
				session.delete(objToDelete);
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception e)
		{
			return false;
		}
	}


	/**
	 * returns a single element searched by its id (key)
	 * 
	 * @param	className	The Class to search in
	 * @param	id			The id of the element
	 * @return	NULL if not found, else the Object
	 * @author Rachit
	 */
	@Override
	public Object findById(Class class1,Long id) {
		try
		{
			List<Object> list = findByProperty(class1,"id",id);
			if(list.size()>0)	
				return list.get(0);	
			else
				return null;
		}
		catch(Exception e)
		{
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) 
	public List<Object> findByProperty(Class class1,String propertyName,Object propertyValue)
	{	
		Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(class1);
		List<Object> list = criteria.add(Restrictions.eq(propertyName, propertyValue)).list();
		return list;
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED) 
	public List<Object> findByProperty(Class class1,String propertyName1,Object propertyValue1,String propertyName2,Object propertyValue2)
	{	
		Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(class1);
		List<Object> list = criteria.add(Restrictions.eq(propertyName1, propertyValue1)).add(Restrictions.eq(propertyName2, propertyValue2)).list();
		return list;
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED) 
	public List<Object> getAll(Class class1){
		Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(class1);
		List<Object> list = criteria.list();
		return list;
	}

}
